package algorithm.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author dengzhiyuan
 * @version 1.0
 * @time 2021/6/11 18:45
 */
public class TreePrint {

    public static void main(String[] args) {
        TreePrint treePrint = new TreePrint();
        treePrint.postPrintV2(treePrint.createTree());
    }

    /**
     * 前序遍历(根节点→左子树→右子树)
     * @param root
     */
    private void printPre(TreeNode root){
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        //1,边界条件判断
        //2,栈不能为空
        while (root != null || !stack.isEmpty() ){
            while (root != null){
                stack.push(root);
                result.add(root.val);
                root = root.left;
            }
            root = stack.pop().right;
        }
        result.forEach(e-> System.out.println(e));
    }

    /**
     * 中序遍历(左子树→根节点→右子树)
     * @param root
     */
    private void printMiddle(TreeNode root){
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        //1,边界条件判断
        //2,栈不能为空
        while (root != null || !stack.isEmpty() ){
            while (root != null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            result.add(root.val);
            root = root.right;
        }
        result.forEach(e-> System.out.println(e));
    }

    /**
     * 后序遍历(左-右-根)
     * @param root
     */
    private void postPrint(TreeNode root){
        List<Integer> result = new ArrayList<>();
        this.postOrder(root,result);
        result.forEach(e-> System.out.println(e));
    }

    /**
     * 后序遍历(左-右-根)
     * @param root
     */
    private void postPrintV2(TreeNode root){
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            root = stack.pop();
            if(root == null){
                result.add(stack.pop().val);
            }else{
                stack.push(root);
                stack.push(null);
                if(root.right != null){
                    stack.push(root.right);
                }
                if(root.left != null){
                    stack.push(root.left);
                }
            }
        }
        result.forEach(e-> System.out.println(e));
    }

    private void postOrder(TreeNode root,List<Integer> result){
        if(root == null){
            return;
        }
        this.postOrder(root.left,result);
        this.postOrder(root.right, result);
        if(root != null){
            result.add(root.val);
        }
    }


    private TreeNode createTree(){
//        Integer[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Integer[] arr = {1,2,3,4,5,6};
        TreeNode root = convert(0,arr);
        return root;
    }
    
    private TreeNode convert(int position,Integer[] arr){
        if(position >=  arr.length){
            return null;
        }
        TreeNode treeNode = new TreeNode(arr[position]);
        treeNode.left = convert(2*position +1, arr);
        treeNode.right = convert(2*position +2, arr);
        return treeNode;
    }

}
